<template>
  <div class="bgimg">
    <el-container>
	<el-header></el-header>
		<!-- <router-link :to="{path:'/blogs'}"><img class="mlogo" src="https://s1.ax1x.com/2022/07/08/jBD2n0.png" alt="jBD2n0.png" border="0" /></router-link> -->
  
      <el-main>
		  <!-- Row -->
		  <div class="simple-grid">
		      <!--Cell with given width. Replace 25% with whatever you want -->
		      <div class="simple-grid__cell simple-grid__cell--1/2">
				  <div 	v-show="imgNode" style="text-align: center;">
				  <el-image
				    style="width: 50%; height: 50%;margin: 50px auto;"
				    :src="loginimagurl"
				    fit="fill"></el-image>
					</div>
			  </div>
		      <!-- Cell that takes remaining width -->
		      <div class="simple-grid__cell simple-grid__cell--fill loginbox">
				  <!-- 登录 -->
				  <el-form v-if="!register" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
				    <el-form-item label="用户名" prop="username" >
				      <el-input v-model="ruleForm.username" clearable></el-input>
				    </el-form-item>
				    <el-form-item label="密码" prop="password">
				      <el-input type="password" v-model="ruleForm.password" show-password></el-input> 
				    </el-form-item>
				  
				    <el-form-item>
				      <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
				      <el-button @click="resetForm('ruleForm')">重置</el-button>
					  &nbsp;<el-link @click="register= !register">没有账号？去注册</el-link>
				    </el-form-item>
					<el-form-item>
					  &nbsp;<el-link @click="getImgLogin()">扫码登录</el-link>
					</el-form-item>
				  </el-form>
				  <!-- 注册 -->
				  <el-form v-else :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
				    <el-form-item label="用户名" prop="username" >
				      <el-input v-model="registerForm.username" clearable></el-input>
				    </el-form-item>
				    <el-form-item label="密码" prop="password">
				      <el-input type="password" v-model="registerForm.password" show-password></el-input> 
				    </el-form-item>
					<el-form-item label="确认密码" prop="password">
					  <el-input type="password" v-model="registerForm.repassword" show-password></el-input> 
					</el-form-item>
					<el-form-item label="邮箱" prop="email">
					  <el-input  v-model="registerForm.email" ></el-input> 
					</el-form-item>
					<el-form-item label="电话号码" prop="tel">
					  <el-input v-model="registerForm.tel" ></el-input> 
					</el-form-item>
				  
				    <el-form-item>
				      <el-button type="primary" @click="registerFun('ruleForm')">注册</el-button>
				      <el-button @click="resetForm('ruleForm')">重置</el-button>
				  					  &nbsp;<el-link @click="register= !register">已有账号？去登录</el-link>
				    </el-form-item>
				  </el-form>
				  
			  </div>
		  </div>  
      </el-main>
    </el-container>

  </div>
</template>

<script> 
  export default {
    name: "Login",
    data() {
      return {
		  register:false,
        ruleForm: {
          username: 'vue123456',
          password: '123456'  //密码是123456 md5加密  96e79218965eb72c92a549dd5a330112  111111
        },
		registerForm: {
		  username: '',
		  password: '' ,
		  repassword:'',
		  email:'',
		  tel:''
		  
		},
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'change' }
          ]
        },
		imgNode:false,
		loginimagurl:"",
		timer:null
      };
    },
    methods: {
		getImgLogin(){
			if(this.imgNode){
				clearInterval(this.timer)
				this.timer = null
				return
			}
			const _this = this
			_this.imgNode = !this.imgNode
			this.timer = setInterval(()=>{
				this.$axios.post('/login/imgloginpd/'+1).then(res => {
					if(res.data.data.status==1){
						this.$axios.post('/login', {
										  username: 'vue123456',
										  password: '123456' 
										}).then(res => {
						  // res是axios请求后返回的promise对象
						  const jwt = res.headers['authorization']
						  const userInfo = res.data.data
						
						  // 把数据共享出去
						  _this.$store.commit("SET_TOKEN", jwt)
						  _this.$store.commit("SET_USERINFO", userInfo)
						  _this.$router.push("/blogs")  //跳转路由
						})
						clearInterval(this.timer)
					}
				})

			},2000)
		},
		registerFun(formName){
			this.$message.error('请提交合法的注册信息');
		},
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const _this = this
            //post请求URL后面可以传入一个对象
            this.$axios.post('/login', this.ruleForm).then(res => {
              // res是axios请求后返回的promise对象
              const jwt = res.headers['authorization']
              const userInfo = res.data.data

              // 把数据共享出去
              _this.$store.commit("SET_TOKEN", jwt)
              _this.$store.commit("SET_USERINFO", userInfo) 
              _this.$router.push("/blogs")  //跳转路由
            })

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        // this.$refs[formName].resetFields();
		this.ruleForm = {}
		this.registerForm= {}
      }
    }
  }
</script>

<style scoped> 
	.bgimg{
		background-image: url(https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fefbe2e38ffd7b8094f39349e55dfe551dc0638986921-8wsnbg_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1672990470&t=b39d2f772890816fe08a756b27ccd05d);		background-image: url(	background-image: url(https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fefbe2e38ffd7b8094f39349e55dfe551dc0638986921-8wsnbg_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1672990470&t=b39d2f772890816fe08a756b27ccd05d););
		background-size: 100%;
		background-repeat: no-repeat;
	}
@media screen and (min-width:700px)  {

	.loginbox{
		 background-color:#89e2ff38;
		 box-shadow: 0px 0px 15px #f0f0f0; 
		 border-radius: 10px;
	} 

	.el-header{
		
	}
   .el-footer {
    background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
  }

  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    text-align: center;
    line-height: 200px;
  }

  .el-main {
    /*background-color: #E9EEF3;*/
    color: #333;
    /* text-align: center; */
    /* line-height: 160px; */
	min-height: 400px;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }

  .mlogo {
    height: 60%;
    margin-top: 10px;
  }

  .demo-ruleForm {
    max-width: 500px;
    margin: 50px auto;
  }


.simple-grid {
    display: flex;

    margin-left: -0.25rem;
    margin-right: -0.25rem;
}

.simple-grid__cell {
    padding-left: 0.25rem;
    padding-right: 0.25rem;
}

.simple-grid__cell--fill {
    flex: 1;
}

/* Cell with given width */
.simple-grid__cell--1\/2 {
    flex: 0 0 50%;
}
.simple-grid__cell--1\/3 {
    flex: 0 0 33.3333333%;
}
.simple-grid__cell--1\/4 {
    flex: 0 0 25%;
}

	
}
</style>